//
//  URLSessionTestsViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit

class URLSessionTestsViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!

    let array: [String] = [
        "40.1.将结果转为 JSON 对象"
        ,"40.2.结果转JSON案例"
        ,"40.3.结果转自定义对象案例"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "40.URLSession的使用2：结果处理、模型转换"
        view.backgroundColor = .white
    }

}

extension URLSessionTestsViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.row {
        case 0:
            let vc = URLSessionTests1ViewController()
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case 1:
            let vc = URLSessionTests2ViewController(nibName: "URLSessionTests2ViewController", bundle: nil)
            self.navigationController?.pushViewController(vc, animated: true)
            break
        case 2:
            let vc = URLSessionTests3ViewController(nibName: "URLSessionTests3ViewController", bundle: nil)
            self.navigationController?.pushViewController(vc, animated: true)
            break
        default:
            break
        }
    }
}

extension URLSessionTestsViewController: UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return array.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = array[indexPath.row]
        return cell!
    }
}
